// vite-plugin-mock 切记，不能引用项目 node
// import { API } from "types";

export const hash = (str: string) => {
  let hash = 5381;
  let i = str.length;

  while (i) {
    hash = (hash * 33) ^ str.charCodeAt(--i);
  }
  return String(hash >>> 0);
};

// MyResponse<T = any>
export function resultSuccess<T>(result: T) {
  return {
    code: "SUCCESS",
    data: {
      dataInfo: result,
    },
  };
}

export const page = (pageSize: number, pageNum: number) => {
  return {
    take: pageSize,
    skip: (pageNum - 1) * pageSize,
  };
};

// eslint-disable-next-line max-params
export function resultPageSuccess<T>(
  page: number,
  pageSize: number,
  list: T,
  totalCount: number,
) {
  const pageInfo = pagination(page, pageSize, totalCount);

  return {
    code: "SUCCESS",
    data: {
      dataInfo: list,
      pageInfo: pageInfo,
    },
  };
}

export function resultError(
  message = "Request failed",
  { code = -1, result = null } = {},
) {
  return {
    code,
    result,
    message,
    type: "error",
  };
}

export function pagination(
  pageNum: number,
  pageSize: number,
  totalCount: number,
) {
  const pageInfo = {
    pageNum: pageNum,
    pageSize: pageSize,
    totalCount: totalCount,
    totalPageNum: Math.ceil(totalCount / pageSize),
  };
  return pageInfo;
}

export interface requestParams {
  url: string;
  method: string;
  body: any;
  headers?: { authorization?: string };
  query: any;
}

/**
 * @description 本函数用于从request数据中获取token，请根据项目的实际情况修改
 *
 */
export function getRequestToken({
  headers,
}: requestParams): string | undefined {
  return headers?.authorization;
}
